package com.bibrApp.difu.actvity.others;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class WebActivity extends BaseActivity<OtherPresenter> implements IOtherView {


    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @Override
    protected void initPresenter() {
        presenter = new OtherPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void initView() {
        WebSettings settings = webView.getSettings();
        //settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        //settings.setUseWideViewPort(true);
        settings.setSupportZoom(true);
        settings.setTextZoom(100);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setDatabaseEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        WebChromeClient webChromeClient = new WebChromeClient(){
            @Override
            public void onPermissionRequest(PermissionRequest request) {
                super.onPermissionRequest(request);
                showToast("onPermissionRequest");
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                showToast("onJsAlert");
                return super.onJsAlert(view, url, message, result);
            }
        };
        webView.setWebChromeClient(webChromeClient);

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        String title = intent.getStringExtra("title");
        if(!TextUtils.isEmpty(title)){
            tvTitle.setText(title);
        }
        if(!TextUtils.isEmpty(url)){
            webView.loadUrl(url);
        }else {
            if(title.equals("用户协议")){
                webView.loadUrl("file:////android_asset/userprotocol.html");
            }
        }

    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
